package models

import (
"fmt"


"GoFind/helper"


"github.com/jinzhu/gorm"
)

type UserAccountModel struct {
	BaseModel
	tableName string
}

type UserAccount struct {
	ID        uint64 `gorm:"primary_key"`
	UID       string
	Phone     string `valid:"Required;Mobile"`
	Avatar    uint64
	Nickname  string `valid:"Required;MaxSize(12)"`
	Email     string `valid:"Email"`
	Password  string `valid:"Required;MinSize(6)"`
	// Authentic int8
	// Status    int8
	// CreatedAt string
	// UpdatedAt string
	// UnlockAt  string
}

// 设置 UserAccount 的表名为`user_account`
func (UserAccount) TableName() string {
	return "user_account"
}

func (user *UserAccount) AfterCreate(scope *gorm.DB) error {
	scope.Model(user).Where("id = ?", user.ID).Update("UID", helper.BuildUID(user.ID, 1))
	return nil
}

func (model *UserAccountModel) Test() {
	user := []UserAccount{}
	model.DB().Find(&user)
	fmt.Println(user)
	// defer func() {
	// 	if p := recover(); p != nil {
	// 		fmt.Printf("panic recover! p: %v", p)
	// 		debug.PrintStack()
	// 	}
	// }()
}

func (model *UserAccountModel) CreateUser(user UserAccount) bool {
	user.Password = helper.MD5(user.Password)
	user.Avatar = 1
	model.DB().Exec("UPDATE user_account SET nickname=? WHERE id=(?)", user.Nickname, 1)
	fmt.Println(user)
	// err := model.DB().Create(&user);
	// fmt.Println(err.Error)
	// if  err == nil {
	// 	return false
	// }
	return true
}
